package com.pulaien.web.controller.customermessage;

import com.github.pagehelper.PageInfo;
import com.pulaien.common.annotation.Anonymous;
import com.pulaien.common.core.controller.BaseController;
import com.pulaien.common.core.domain.R;
import com.pulaien.common.core.page.TableDataInfo;
import com.pulaien.common.utils.ConstantsUtil;
import com.pulaien.common.utils.date.DateFormat;
import com.pulaien.common.utils.date.DateUtils;
import com.pulaien.common.utils.date.EnumDateStyle;
import com.pulaien.system.domain.MessageReminding;
import com.pulaien.system.domain.dto.admin.ExtMessageReminding;
import com.pulaien.system.service.IMessageRemindingService;
import com.pulaien.system.service.customermessage.CustomerMessageRemindingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author yuanlai
 * @create 20230403 10:06
 **/

@Api(tags = "CustomerMessageRemindingController",description = "买家端-消息通知")
@RestController
@Anonymous
@RequestMapping("/shop/message")

public class CustomerMessageRemindingController extends BaseController {


    private final IMessageRemindingService messageRemindingService;
    private final CustomerMessageRemindingService customerMessageRemindingService;

    public CustomerMessageRemindingController(
            IMessageRemindingService messageRemindingService,
            CustomerMessageRemindingService customerMessageRemindingService
    ) {
        this.messageRemindingService = messageRemindingService;
        this.customerMessageRemindingService = customerMessageRemindingService;
    }



    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前记录起始索引", paramType = "query", defaultValue = "1", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", paramType = "query", defaultValue = "10", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "orderByColumn", value = "排序列", paramType = "query"),
            @ApiImplicitParam(name = "isAsc", value = "排序的方向 \"desc\" 或者 \"asc\"", paramType = "query"),
            @ApiImplicitParam(name = "reasonable", value = "分页参数合理化", paramType = "query", dataType = "Boolean")
    })
    @ApiOperation(value = "消息列表分页展示")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public TableDataInfo list(
            ExtMessageReminding message
    ) {

        startPage();
        List<MessageReminding> record = customerMessageRemindingService.list(message);

        List<MessageReminding> record_new = new ArrayList<>();
        record.forEach(
                messageReminding -> {
                    List<Map> fileList = new ArrayList<>();
                    List<Map> fileList_new = new ArrayList<>();

                    if(StringUtils.isNotBlank(messageReminding.getFileName())) {
                        List<String> collect = Arrays.stream(messageReminding.getFileName().split(",")).collect(Collectors.toList());
                        messageReminding.setFileNameList(collect);
                        collect.forEach(
                                c -> {
                                    Map<String,String> map = new HashMap<>();
                                    map.put("name" , c);
                                    fileList.add(map);
                                }
                        );
                    }

                    if(StringUtils.isNotBlank(messageReminding.getFilePath())) {
                        List<String> collect = Arrays.stream(messageReminding.getFilePath().split(",")).collect(Collectors.toList());
                        messageReminding.setFilePathList(collect);
                        for(int i =0 ; i < collect.size() ; i++  ) {
                            Map map = fileList.get(i);
                            map.put("path" , collect.get(i)  );
                            fileList_new.add(map);
                        }
                    }

                    if(StringUtils.isNotBlank(messageReminding.getReplyPicture())) {
                        List<String> collect = Arrays.stream(messageReminding.getReplyPicture().split(",")).collect(Collectors.toList());
                        messageReminding.setReplyPictureList(collect);
                    }

                    messageReminding.setFileList(fileList_new);
                    record_new.add(messageReminding);
                }
        );

        return getDataTable(record_new , new PageInfo(record).getTotal());
    }



    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前记录起始索引", paramType = "query", defaultValue = "1", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", paramType = "query", defaultValue = "10", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "orderByColumn", value = "排序列", paramType = "query"),
            @ApiImplicitParam(name = "isAsc", value = "排序的方向 \"desc\" 或者 \"asc\"", paramType = "query"),
            @ApiImplicitParam(name = "reasonable", value = "分页参数合理化", paramType = "query", dataType = "Boolean")
    })
    @ApiOperation(value = "消息列表分页展示")
    @RequestMapping(value = "/me/list", method = RequestMethod.GET)
    public TableDataInfo meList(
            ExtMessageReminding message
    ) {

        startPage();
        List<MessageReminding> record = customerMessageRemindingService.meList(message);

        List<MessageReminding> record_new = new ArrayList<>();
        record.forEach(
                messageReminding -> {
                    List<Map> fileList = new ArrayList<>();
                    List<Map> fileList_new = new ArrayList<>();

                    if(StringUtils.isNotBlank(messageReminding.getFileName())) {
                        List<String> collect = Arrays.stream(messageReminding.getFileName().split(",")).collect(Collectors.toList());
                        messageReminding.setFileNameList(collect);
                        collect.forEach(
                                c -> {
                                    Map<String,String> map = new HashMap<>();
                                    map.put("name" , c);
                                    fileList.add(map);
                                }
                        );
                    }

                    if(StringUtils.isNotBlank(messageReminding.getFilePath())) {
                        List<String> collect = Arrays.stream(messageReminding.getFilePath().split(",")).collect(Collectors.toList());
                        messageReminding.setFilePathList(collect);
                        for(int i =0 ; i < collect.size() ; i++  ) {
                            Map map = fileList.get(i);
                            map.put("path" , collect.get(i)  );
                            fileList_new.add(map);
                        }
                    }

                    if(StringUtils.isNotBlank(messageReminding.getReplyPicture())) {
                        List<String> collect = Arrays.stream(messageReminding.getReplyPicture().split(",")).collect(Collectors.toList());
                        messageReminding.setReplyPictureList(collect);
                    }

                    messageReminding.setFileList(fileList_new);
                    record_new.add(messageReminding);
                }
        );

        return getDataTable(record_new , new PageInfo(record).getTotal());
    }





    @ApiOperation("全部已读")
    @PostMapping(value = "/readAll")
    public R<Integer> readAll() {
        int count = customerMessageRemindingService.updateStatusAll();
        if (count > 0) {
            return R.ok(count);
        }
        return R.ok(0);
    }


    @ApiOperation(value = "单条已读")
    @PostMapping(value = "/read")
    public R read(
            @RequestBody MessageReminding message
    ) {
        int count = messageRemindingService.updateStatus(message);
        if (count > 0) {
            return R.ok(count);
        }
        return R.fail();
    }

    @ApiOperation(value = "详情并且已读")
    @GetMapping(value = "/info")
    public R<ExtMessageReminding> info(
            HttpServletRequest request,
            @RequestBody MessageReminding message
    ) {
        String lang = request.getHeader(ConstantsUtil.HEAD_LANGUAGE);
        MessageReminding record=messageRemindingService.getRecordById(message);
        ExtMessageReminding extRecord = new ExtMessageReminding();
        BeanUtils.copyProperties(record,extRecord);
        //多语言处理
        //消息提醒多语言处理 ？
        /*if (StringUtils.isNotEmpty(lang)){
            switch (lang){
                case ConstantsUtil.LANG_EN:
                    extRecord.setRemindingTitle(record.getRemindingTitleEn());
                    extRecord.setRemindingContent(record.getRemindingContentEn());
                    break;
                case ConstantsUtil.LANG_JA:
                    extRecord.setRemindingTitle(record.getRemindingTitleJa());
                    extRecord.setRemindingContent(record.getRemindingContentJa());
                    break;
                default:
                    extRecord.setRemindingTitle(record.getRemindingTitleZh());
                    extRecord.setRemindingContent(record.getRemindingContentZh());
            }
        }else {
            extRecord.setRemindingTitle(record.getRemindingTitleZh());
            extRecord.setRemindingContent(record.getRemindingContentZh());
        }*/
        return R.ok(extRecord);
    }


    @ApiOperation("未读数量")
    @GetMapping(value = "/unreadCount")
    public R<Integer> unreadCount(){
        return customerMessageRemindingService.unreadCount();
    }


    /**
     * 设置检索条件
     *
     * @param condition
     */
    private void setCondition(ExtMessageReminding condition) {

        // 下单时间
        if (StringUtils.isNotEmpty(condition.getCreateTimeStart())) {
            condition.setCreateTimeStart(String.valueOf(DateUtils.stringToLong(DateFormat.getDayStart(condition.getCreateTimeStart()), EnumDateStyle.YYYY_MM_DD_HH_MM_SS.getValue())));
        }
        if (StringUtils.isNotEmpty(condition.getCreateTimeEnd())) {
            condition.setCreateTimeEnd(String.valueOf(DateUtils.stringToLong(DateFormat.getDayEnd(condition.getCreateTimeEnd()), EnumDateStyle.YYYY_MM_DD_HH_MM_SS.getValue())));
        }
    }







/*    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前记录起始索引", paramType = "query", defaultValue = "1", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", paramType = "query", defaultValue = "10", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "orderByColumn", value = "排序列", paramType = "query"),
            @ApiImplicitParam(name = "isAsc", value = "排序的方向 \"desc\" 或者 \"asc\"", paramType = "query"),
            @ApiImplicitParam(name = "reasonable", value = "分页参数合理化", paramType = "query", dataType = "Boolean")
    })
    @ApiOperation(value = "全端所有消息")
    @RequestMapping(value = "/allMessage", method = RequestMethod.GET)
    public TableDataInfo allMessage(
            ExtMessageReminding message
    ) {

        this.setCondition(message);
        startPage();
        List<MallMessageReminding> record = messageRemindingService.allMessage(message);
        return getDataTable(record);
    }*/












}
